//
//  VisiterView.swift
//  上海微博
//
//  Created by zhangliming on 16/2/25.
//  Copyright © 2016年 zhangliming. All rights reserved.
//

import UIKit

import SnapKit

///访客视图协议／协议必须继承自NSObjectProtocol否则无法用 weak

//protocol VisiterViewDelegete :NSObjectProtocol {
//
//    func didClickrigisterButton ()
//
//    func didClickLoginButton ()
//
//}

class VisiterView: UIView {

    ///定义代理

//    weak var delegete:VisiterViewDelegete?

    ///登录按钮

    lazy var loginButton :UIButton = {

        let loginButton = UIButton.zh_UIButton(bgImageName: "common_button_white_disable",  title: "登录", titleColor: UIColor.orangeColor())

        return loginButton
    }()

    ///注册按钮

    lazy var rigisterButton :UIButton = {

        let rigisterButton = UIButton.zh_UIButton(bgImageName: "common_button_white_disable", title: "注册", titleColor: UIColor.orangeColor())

        return rigisterButton
    }()



    //MARK:-生命周期方法

   override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)

        setUpVistorView()

    ///添加监听方法

//    loginButton.addTarget(self, action:"ClickLoginButtton" , forControlEvents: .TouchUpInside)
//
//    rigisterButton.addTarget(self, action: "ClickRigsterButton", forControlEvents: .TouchUpInside)

        
    }



    //如果有试图通过xib来使用此类就会崩溃

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }

    //MARK:-懒加载私有控件

   private lazy var wheelView = UIImageView.zh_UIimageView()

   private lazy var homeView = UIImageView.zh_UIimageView()

    
   private lazy var coverView = UIImageView.zh_UIimageView("visitordiscover_feed_mask_smallicon")

    ///描述label

   private lazy var detileLable : UILabel = {

        let detileLable = UILabel()

        detileLable.text = ""

        detileLable.numberOfLines = 0

        detileLable.textColor = UIColor.purpleColor()

        detileLable.font = UIFont.systemFontOfSize(14)

        detileLable.textAlignment = .Center

        detileLable.sizeToFit()

        return detileLable

        }()

    ///登录按钮



}

// VisiterView 方法

extension VisiterView {

    ///点击登陆按钮代理写法

//  @objc private func ClickLoginButtton (){
//
//    delegete?.didClickLoginButton()
//
//    }
//
//    ///点击注册按钮
//
//   @objc private func ClickRigsterButton (){
//
//    delegete?.didClickrigisterButton()
//
//    }

    //MARK:-添加访客视图子控件

    @objc private func setUpVistorView (){

        addSubview(wheelView)

        addSubview(coverView)

        addSubview(homeView)

        addSubview(detileLable)

        addSubview(loginButton)

        addSubview(rigisterButton)


///自动布局苹果原生的自动布局

        for v in subviews {

            ///关闭所有子控件的输入frame布局模式

            v.translatesAutoresizingMaskIntoConstraints = false

        }

        addConstraint(NSLayoutConstraint(
            item: wheelView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))

        addConstraint(NSLayoutConstraint(
            item: wheelView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: -60))

        addConstraint(NSLayoutConstraint(
            item: homeView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: wheelView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))

        addConstraint(NSLayoutConstraint(
            item: homeView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: wheelView,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: 30))

        addConstraint(NSLayoutConstraint(
            item: detileLable,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: homeView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))

        addConstraint(NSLayoutConstraint(
            item: detileLable,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: homeView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant:50))

        addConstraint(NSLayoutConstraint(
            item: detileLable,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: .None,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 224))

        ///snapKit框架自动布局

        loginButton.snp_makeConstraints { (make) -> Void in

            make.right.equalTo(detileLable)

            make.top.equalTo(detileLable.snp_bottom).offset(12)

            make.size.equalTo(CGSizeMake(100, 36))
        }

        rigisterButton.snp_makeConstraints { (make) -> Void in

            make.left.equalTo(detileLable)

            make.top.equalTo(loginButton)

            make.size.equalTo(loginButton)
        }

        coverView.snp_makeConstraints { (make) -> Void in

            make.bottom.equalTo(detileLable.snp_top).offset(-30)

            make.left.equalTo(wheelView)

            make.size.equalTo(CGSizeMake(200 , 200))

        }

    }

     //MARK:-设置访客视图

    func setUpUI (title title:String, homeImageName:String, wheelImageName:String?){

        detileLable.text = title

        homeView.image = UIImage(named: homeImageName)

        guard let ImageName = wheelImageName else{

            wheelView.hidden = true

            coverView.hidden = true

            return

        }

        wheelViewAnimation()


        wheelView.image = UIImage(named: ImageName)

            }

    //MARK:-转轮动画

    func wheelViewAnimation (){

        let anim = CABasicAnimation(keyPath:"transform.rotation" )

        anim.toValue = 2 * M_PI

        anim.duration = 10

        anim.repeatCount = MAXFLOAT

        anim.removedOnCompletion = false

        wheelView.layer.addAnimation(anim, forKey: nil)

    }

}
